<template>
  <div class="mod-attr">
    <el-container>
      <el-aside style="width: 20%; min-width: 200px">
        <el-card class="box-card">
          <template #header>
            <div class="clearfix">
              <span class="a-title">商品分类</span>
            </div>
          </template>
          <category-tree @node-click="treeNodeClickEvent"></category-tree>
        </el-card>
      </el-aside>
      <el-main style="padding: 0 20px">
        <el-card v-if="table.visible" class="box-card">
          <template #header>
            <div class="clearfix">
              <span class="a-title">商品分类[{{ tree.selectData.name }}]-属性列表</span>
            </div>
          </template>
          <data-table ref="dataTableRef" :criterias="table.criterias" :name="'attr' + type" :initLoad="true" :url="table.url">
            <template v-slot:search="criterias">
              <dt-search-item label="属性名" prop="name">
                <el-input v-model="criterias.name.value" clearable maxlength="50" placeholder="请输入属性名"></el-input>
              </dt-search-item>
            </template>
            <!-- ------------------------------------------ 工具栏 ------------------------------------------ -->
            <template v-slot:toolbar>
              <el-button-group>
                <el-button v-if="admin.isAuth('product:attr:save')" @click="() => addAttrEvent()" type="primary">
                  新增
                </el-button>
                <el-button v-if="admin.isAuth('product:attr:save')" @click="() => updateAttrEvent()" type="warning">
                  修改
                </el-button>
                <el-button v-if="admin.isAuth('product:attr:delete')" @click="() => deleteAttrEvent()" type="danger">
                  删除
                </el-button>
              </el-button-group>
            </template>
            <!-- ---------------------------------------- 数据表格列 ---------------------------------------- -->
            <el-table-column fixed prop="name" show-overflow-tooltip header-align="center" align="left" label="属性名" width="180" />
            <el-table-column v-if="type === 1" prop="attrgroupName" show-overflow-tooltip header-align="center" align="left" label="属性分组" width="180" />
            <el-table-column prop="options" header-align="center" align="left" label="可选值列表">
              <template #default="scope">
                <span v-for="(i, index) in scope.row.options.split(';')" :key="index">
                  <el-tag>{{ i }}</el-tag
                  >&nbsp;
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="multiple" header-align="center" align="center" label="启用多选" width="100">
              <template #default="scope">
                <el-tag v-if="scope.row.multiple === 0" size="small" type="danger">否</el-tag>
                <el-tag v-else size="small">是</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="visible" header-align="center" align="center" label="快速展示" width="100">
              <template #default="scope">
                <el-tag v-if="scope.row.visible === 0" size="small" type="danger">否</el-tag>
                <el-tag v-else size="small">是</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="enable" header-align="center" align="center" label="启用状态" width="100">
              <template #default="scope">
                <el-tag v-if="scope.row.enable === 0" size="small" type="danger">禁用</el-tag>
                <el-tag v-else size="small">启用</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="searchable" header-align="center" align="center" label="提供检索" width="100">
              <template #default="scope">
                <el-tag v-if="scope.row.searchable === 0" size="small" type="danger">不提供</el-tag>
                <el-tag v-else size="small">提供</el-tag>
              </template>
            </el-table-column>
          </data-table>
        </el-card>
      </el-main>
    </el-container>
    <!-- 弹窗, 新增 / 修改 -->
    <attr-add-or-update v-if="addOrUpdateVisible" :type="type" ref="addOrUpdateRef" @loadAttrData="loadAttrData"></attr-add-or-update>
  </div>
</template>

<script lang="ts" setup>
import admin from "@/utils/admin";
import http from "@/utils/httpRequest";
import { ref, reactive, watch, nextTick } from "vue";
import cloneDeep from "lodash/cloneDeep";
import { ElTree, ElMessage, ElMessageBox } from "element-plus";

import CategoryTree from "../common/category-tree.vue";
import AttrAddOrUpdate from "./attr-add-or-update.vue";

// 属性定义
const props = defineProps({
  type: {
    type: Number,
    default: 1,
  },
});

// 分类树形结构
const tree = reactive({
  // 当前树选中的节点
  selectData: undefined,
});

// 部门人员关系列表
const table = reactive({
  // 查询条件
  criterias: {
    name: {
      type: "ll",
      value: "",
    },
    categoryId: undefined,
    type: undefined,
  },
  // 列表是否显示
  visible: false,
  // 属性分组下拉数据
  attrGroupList: [],
  // 请求地址
  url: "",
});

// 数据表格引用
const dataTableRef = ref();

// 弹窗引用
const addOrUpdateRef = ref();

// 新增修改弹窗是否显示
const addOrUpdateVisible = ref(false);

// 分类关联窗口是否显示
const categoryCascaderVisible = ref(false);

// 分类节点点击事件
const treeNodeClickEvent = (data) => {
  if (data.attributes && data.attributes.level === 3) {
    tree.selectData = data;
    loadAttrData();
  }
};

// 初始化属性列表
const loadAttrData = async () => {
  if (!tree.selectData) {
    return;
  }
  table.url = `/product/attr/select`;
  table.visible = true;
  table.criterias.categoryId = tree.selectData.id;
  table.criterias.type = props.type;
  await nextTick();
  dataTableRef.value.loadTableData();
};

// 新增属性
const addAttrEvent = async () => {
  if (!tree.selectData) {
    ElMessage.error("请先选择新增规则参数所属分类！");
    return;
  }
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm("add", undefined, tree.selectData);
};

// 修改属性
const updateAttrEvent = async () => {
  if (!tree.selectData) {
    ElMessage.error("请先选择新增规则参数所属分类！");
    return;
  }
  let data = admin.obtainOneSelectionTableData(dataTableRef.value);
  if (!data) {
    return;
  }
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm("update", data, tree.selectData);
};

// 删除属性
const deleteAttrEvent = () => {
  admin.batchDeleteSelectionTableData(dataTableRef.value, "/product/attr/delete");
};
</script>

<style lang="scss" scoped>
.mod-attr {

  .a-title {
    font-size: 16px;
    font-weight: 600;
  }
}
</style>